package com.xhwl.data.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xhwl.common.dto.cent.sdata.EnterpriseDTO;
import com.xhwl.common.pojo.cent.sdata.EnterprisePermission;
import com.xhwl.common.pojo.cent.sdata.Permission;
import com.xhwl.common.query.cent.sdata.PermissionQuery;
import com.xhwl.data.dao.IEnterprisePermissionDao;
import com.xhwl.data.dao.IPermissionDao;
import com.xhwl.data.service.IEnterprisePermissionService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 企业与权限关联
 * @author: lianghz
 * @create: 2020-11-25
 **/
@Service
public class EnterprisePermissionServiceImpl extends ServiceImpl<IEnterprisePermissionDao, EnterprisePermission> implements IEnterprisePermissionService {

    @Autowired
    IEnterprisePermissionDao enterprisePermissionDao;

    @Autowired
    IPermissionDao permissionDao;

    private static final Logger log = LoggerFactory.getLogger(EnterprisePermissionServiceImpl.class);

    @Override
    public List<Integer> list(List<Integer> enterpriseIdList) {
        QueryWrapper<EnterprisePermission> enterprisePermissionQueryWrapper = new QueryWrapper<>();
        enterprisePermissionQueryWrapper
                .select("permission_id")
                .in("enterprise_id", enterpriseIdList);
        List<EnterprisePermission> enterprisePermissions = this.list(enterprisePermissionQueryWrapper);
        List<Integer> permissionIdList = enterprisePermissions.stream().map(EnterprisePermission::getPermissionId).collect(Collectors.toList());
        return permissionIdList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void save(Integer enterpriseId, List<Integer> permissionIdList) {
        QueryWrapper<EnterprisePermission> enterprisePermissionQueryWrapper = new QueryWrapper<>();
        enterprisePermissionQueryWrapper.eq("enterprise_id", enterpriseId);
        this.remove(enterprisePermissionQueryWrapper);
        List<EnterprisePermission> enterprisePermissions = permissionIdList.stream().map(permissionId -> {
            EnterprisePermission enterprisePermission = new EnterprisePermission();
            enterprisePermission.setPermissionId(permissionId);
            enterprisePermission.setEnterpriseId(enterpriseId);
            return enterprisePermission;
        }).collect(Collectors.toList());
        this.saveBatch(enterprisePermissions);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteEnterprisePermission(List<Integer> enterpriseIdList, List<Integer> permissionIdList) {
        UpdateWrapper updateWrapper = new UpdateWrapper();
        if (!CollectionUtils.isEmpty(enterpriseIdList)) {
            updateWrapper.in("enterprise_id", enterpriseIdList);
        }
        if (!CollectionUtils.isEmpty(permissionIdList)) {
            updateWrapper.in("permission_id", permissionIdList);
        }
        enterprisePermissionDao.delete(updateWrapper);
    }

    @Override
    public void saveV2(EnterpriseDTO enterpriseDTO, List<Integer> permissionIdList) {
        // 是否只修改当前业态的权限
        /*if(null != enterpriseDTO.getUpdateCurrentIndustry()){
            PermissionQuery permissionQuery = new PermissionQuery();
            permissionQuery.setIndustryId(enterpriseDTO.getUpdateCurrentIndustry().shortValue());
            permissionQuery.setEnterpriseId(enterpriseDTO.getId());
            enterprisePermissionDao.remove(permissionQuery);
        }else{*/
            QueryWrapper<EnterprisePermission> enterprisePermissionQueryWrapper = new QueryWrapper<>();
            enterprisePermissionQueryWrapper.eq("enterprise_id", enterpriseDTO.getId());
            this.remove(enterprisePermissionQueryWrapper);
        //}
        List<EnterprisePermission> enterprisePermissions = permissionIdList.stream().map(permissionId -> {
            EnterprisePermission enterprisePermission = new EnterprisePermission();
            enterprisePermission.setPermissionId(permissionId);
            enterprisePermission.setEnterpriseId(enterpriseDTO.getId());
            return enterprisePermission;
        }).collect(Collectors.toList());
        this.saveBatch(enterprisePermissions);
    }

}
